
package cn.sh.ideal.web.view.display.controller;

import cn.sh.ideal.web.view.display.entity.ProcessInforEntity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.repository.query.Param;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName
 * @Description Too
 * @Author ME
 * @Data 10:14
 **/



@Controller
@RequestMapping(value ="/data")
public class ProcessCollectingController {

    @Value("${server_address}")
    private String serverAddress;

    private RestTemplate rs = new RestTemplate();



    @RequestMapping(value = "/getProcessConfig",method =RequestMethod.POST)
    @ResponseBody
    public  String  getProcessConfig(@Param("ip")String ip,
                                     @Param("port")String port,
                                     @Param("type")String type){
        if("2".equals(type)&& StringUtils.isNotBlank(type)){
            String url = serverAddress+"/getCommandConfig?ip="+ip+"&port="+stringToUnicode(port)+"&type="+type;
            HttpHeaders headers = new HttpHeaders();
            MediaType types = MediaType.parseMediaType("application/json;charset=UTF-8");
            headers.setContentType(types);
            HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
            ResponseEntity<String> exchange = rs.exchange(url,HttpMethod.GET, entity, String.class);
            String result = exchange.getBody();
            return result;
        }else{
            String url = serverAddress+"/getProcessConfig?ip="+ip+"&port="+port;
            HttpHeaders headers = new HttpHeaders();
            MediaType types = MediaType.parseMediaType("application/json;charset=UTF-8");
            headers.setContentType(types);
            HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
            ResponseEntity<String> exchange = rs.exchange(url,HttpMethod.GET, entity, String.class);
            String result = exchange.getBody();
            return result;
            
            
            
        }


    }

    @RequestMapping(value="/getProcessAll")
    @ResponseBody
    public String getAllInfor(@RequestParam("ip")String ip){
        System.out.println("===========================");
        String url = serverAddress+"/getProcessAll?ip="+ip;
        System.out.println("====================="+url);
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
        ResponseEntity<String> exchange = rs.exchange(url,HttpMethod.GET, entity, String.class);
        String result = exchange.getBody();
        return result;
    }


    //增加
    @RequestMapping(value="/insertProcess",method=RequestMethod.POST)
    @ResponseBody
    public int insertProcessInfo(@RequestBody List<ProcessInforEntity> processInfor,
                                 HttpServletRequest request,
                                 HttpServletResponse response){
        System.out.println("==========insertProcess start =================");
        ArrayList<ProcessInforEntity> processInforEntityList = (ArrayList<ProcessInforEntity>) JSON.parseObject(String.valueOf(processInfor), new TypeReference<ArrayList<ProcessInforEntity>>(){});

        int dao=0;
        for(int i=0;i<processInforEntityList.size();i++){
            String url = serverAddress+"/insertProcess?id="+processInforEntityList.get(i).getId()
                    +""
                    +"&hostid="+processInforEntityList.get(i).getHostid()
                    +"&ip="+processInforEntityList.get(i).getIp()
                    +"&command="+stringToUnicode(processInforEntityList.get(i).getCommand())
                    +"&type="+processInforEntityList.get(i).getType()
                    +"&desc="+processInforEntityList.get(i).getDesc()
                    +"&purpose="+processInforEntityList.get(i).getPurpose()
                    +"&status="+processInforEntityList.get(i).getStatus()
                    +"&alarmFrequencyId="+processInforEntityList.get(i).getAlarmFrequencyId()
                    +"&times="+processInforEntityList.get(i).getTimes()
                    +"&minutes="+processInforEntityList.get(i).getMinutes();
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
            headers.setContentType(type);
            HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
            ResponseEntity<String> responseEntity = rs.postForEntity(url, entity, String.class);
            String result = responseEntity.getBody();
            if("1".equals(result)&&StringUtils.isNotBlank(result)){
                dao++;
            }
        }
        System.out.println("==========insertProcess end =================");
        return dao;
    }

    //删除

    @RequestMapping(value="/deleteProcess",method=RequestMethod.POST)
    @ResponseBody
    public  int deleteProcessInfo(@RequestBody List<ProcessInforEntity> processInfor,
                                  HttpServletRequest request,
                                  HttpServletResponse response){
        int dao=0;
        ArrayList<ProcessInforEntity> processInforEntityList = (ArrayList<ProcessInforEntity>) JSON.parseObject(String.valueOf(processInfor), new TypeReference<ArrayList<ProcessInforEntity>>(){});

        for(int i=0;i<processInforEntityList.size();i++){
            String url = serverAddress+"/deleteProcess?id="+processInforEntityList.get(i).getId()
                    +"&hostid="+processInforEntityList.get(i).getHostid()
                    +"&ip="+processInforEntityList.get(i).getIp()
                    +"&command="+processInforEntityList.get(i).getCommand()
                    +"&type="+processInforEntityList.get(i).getType()
                    +"&desc="+processInforEntityList.get(i).getDesc()
                    +"&purpose="+processInforEntityList.get(i).getPurpose()
                    +"&status="+processInforEntityList.get(i).getStatus();
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
            headers.setContentType(type);
            HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
            ResponseEntity<String> responseEntity = rs.postForEntity(url, entity, String.class);
            String result = responseEntity.getBody();
            if("1".equals(result)&&StringUtils.isNotBlank(result)){
                dao++;
            }
        }

        return dao;
    }

    //update
    @RequestMapping(value="/updateProcess",method=RequestMethod.POST)
    @ResponseBody
    public int updateProcessInfo(@RequestBody ProcessInforEntity ProcessInforEntity,
                                 HttpServletRequest request,
                                 HttpServletResponse response) {
        String url = serverAddress+"/updateProcess?id="+ProcessInforEntity.getId()
                +"&hostid="+ProcessInforEntity.getHostid()
                +"&ip="+ProcessInforEntity.getIp()
                +"&command="+stringToUnicode(ProcessInforEntity.getCommand())
                +"&type="+ProcessInforEntity.getType()
                +"&desc="+ProcessInforEntity.getDesc()
                +"&purpose="+ProcessInforEntity.getPurpose()
                +"&status="+ProcessInforEntity.getStatus()
                +"&alarmFrequencyId="+ProcessInforEntity.getAlarmFrequencyId()
                +"&times="+ProcessInforEntity.getTimes()
                +"&minutes="+ProcessInforEntity.getMinutes();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
        ResponseEntity<String> responseEntity = rs.postForEntity(url, entity, String.class);
        String result = responseEntity.getBody();
        return Integer.parseInt(result);
    }

    //select
    @RequestMapping(value="/selectHostInfo",method=RequestMethod.POST)
    @ResponseBody
    public String selectHostInfo(@RequestBody ProcessInforEntity ProcessInforEntity,
                                 HttpServletRequest request,
                                 HttpServletResponse response){


        String url = serverAddress+"/selectHostInfo?hostid="+ProcessInforEntity.getHostid();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
        ResponseEntity<String> exchange = rs.exchange(url,HttpMethod.GET, entity, String.class);
        String result = exchange.getBody();
        return result;
        }


    //String 转化为unicode
    public  String stringToUnicode(String string) {

        StringBuffer unicode = new StringBuffer();

        for (int i = 0; i < string.length(); i++) {

            // 取出每一个字符
            char c = string.charAt(i);

            // 转换为unicode
            unicode.append("\\u" + Integer.toHexString(c));
        }

        return unicode.toString();
    }


}
